package cn.kgc.smbmsmodulardemo.controller;

import cn.kgc.smbmsmodulardemo.entity.User;
import cn.kgc.smbmsmodulardemo.service.UserService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.annotation.Resource;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @ApiOperation(value = "用户登录", produces = "application/json", notes = "状态码：10000：登陆成功，10001：用户名或密码不正确")
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Object userLogin(@RequestParam("userCode") String userCode,
                            @RequestParam("userPassword") String userPassword,
                            HttpSession httpSession
                            ) {
        JSONObject jsonObject = new JSONObject();
//
        String data = "";
//
        String message = "";
//
        String statCode = "";
//
        User user = new User();
        user.setUserCode(userCode);
        user.setUserPassword(userPassword);
        User result = userService.userLogin(user);
        if (result != null) {
            data = result.toString();
            message = "成功";
            statCode = "10000";


        } else {
            data = "";
            message = "用户名密码错误";
            statCode = "10001";


        }
        jsonObject.put("data", data);
        jsonObject.put("message", message);
        jsonObject.put("statCode", statCode);
        return jsonObject.toString();


    }
}

